#import "SEGAutoHeightImageView.h"


#import <UIImageView+WebCache.h>
#import "UIImageView+SEGWebCache.h"

#import <Masonry/Masonry.h>

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif


#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

@implementation SEGAutoHeightImageView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kAutoHeightImageViewHeight;
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.contentImageView];
    [self.contentImageView addSubview:self.imageCountButton];

//    self.userInteractionEnabled = YES;
//    self.contentImageView.userInteractionEnabled = YES;
//    __weak typeof(self) _self = self;
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
//        [SEGAlertView showTmepMessageWithMessage:@"SEGAutoHeightImageViewClick"];
//        if (_self.delegate && [_self.delegate respondsToSelector:@selector(autoHeightImageViewTap:)]) {
//            [_self.delegate autoHeightImageViewTap:self.contentImageView];
//        }
//    }];
//    [self addGestureRecognizer:tap];

    return self;
}

-(void)setWithLayout:(SEGAutoHeightImageViewLayout *)layout{
    self.height = layout.totalHeight;
    self.width = layout.data.pictureWidth;
    
    _layout = layout;
    
    if (self.height <= 0) return;
    if (_layout.data.picCount > 1) {
        self.imageCountButton.hidden = NO;
    }else {
        self.imageCountButton.hidden = YES;
    }
    [self.contentImageView setImageWithURL:[NSURL URLWithString:_layout.data.pic] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];
    [self.imageCountButton setTitle:[NSString stringWithFormat:@"%ld",(long)_layout.data.picCount] forState:UIControlStateNormal];
    
    [self setupUI];
}

//界面布局
-(void)setupUI{
    self.contentImageView.frame = self.bounds;
    [self.imageCountButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.contentImageView).offset(-10);
    }];
}

#pragma mark - getter
-(UIImageView *)contentImageView{
    if (!_contentImageView) {
        _contentImageView = [[UIImageView alloc]init];
        _contentImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")];
        _contentImageView.contentMode = UIViewContentModeScaleAspectFill;
        _contentImageView.layer.masksToBounds = YES;
        //_contentImageView.layer.cornerRadius = 0.5;
//        _contentImageView.backgroundColor = kSegRandomColor;
        _contentImageView.userInteractionEnabled = YES;
    }
    return _contentImageView;
}

- (UIButton *)imageCountButton {
    if (!_imageCountButton) {
        _imageCountButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_imageCountButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_pic")] forState:UIControlStateNormal];
        [_imageCountButton setTitle:@"9" forState:UIControlStateNormal];
        [_imageCountButton setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"bg_huati_pic")] forState:UIControlStateNormal];
        _imageCountButton.titleLabel.font = [UIFont systemFontOfSize:11];
        _imageCountButton.imageEdgeInsets = UIEdgeInsetsMake(0, -1.25, 0, 1.25);
        _imageCountButton.titleEdgeInsets = UIEdgeInsetsMake(0, 1.25, 0, -1.25);
    }
    return _imageCountButton;
}

@end

